package com.zhu.nio.channel;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by ehsy_it on 2016/4/12.
 */
public class FileChannelTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("d:/data/nio-data.txt", "rw");
        FileChannel channel = file.getChannel();
        ByteBuffer buff = ByteBuffer.allocate(48);

        int cnt = 0;
        while((cnt = channel.read(buff)) != -1){
           buff.flip();
            //buff.rewind();

            buff.mark();
            System.out.print((char)buff.get());
            buff.reset();
            while(buff.hasRemaining()){
                System.out.print((char)buff.get());
            }

            buff.clear();
        }

        file.close();
    }
}
